#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"

DLTNode* Buy_DLTNode(DListType x)
{
	DLTNode* newnode = (DLTNode*)malloc(sizeof(DLTNode));
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->data = x;
	return newnode;
}


DLTNode* DLTInit()
{
	DLTNode* phead = Buy_DLTNode(0);
	phead->next = phead;
	phead->prev = phead;
	phead->data = 0;
	return phead;
}


void DLTDestroy(DLTNode* phead)
{
	assert(phead);
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		DLTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}

void DLTPrint(DLTNode* phead)
{
	assert(phead);
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

DLTNode* DLTFind(DLTNode* phead, DListType x)
{
	assert(phead);
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;

		}
		cur = cur->next;
	}
	return NULL;
}

void DLTChange(DLTNode* pos, DListType x)
{
	assert(pos);
	DLTNode* cur = pos;
	cur->data = x;
}

void DLTInsert(DLTNode* pos, DListType x)
{
	assert(pos);
	DLTNode* prev = pos->prev;
	DLTNode* newnode = Buy_DLTNode(x);
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev=newnode;
}

void DLTPushBack(DLTNode* phead, DListType x)
{
	assert(phead);
	DLTInsert(phead, x);
}

void DLTPushFront(DLTNode* phead, DListType x)
{
	assert(phead);
	DLTInsert(phead->next, x);
}

void DLTErase(DLTNode* pos)
{
	assert(pos);
	DLTNode* prev = pos->prev;
	DLTNode* next = pos->next;
	prev->next = pos->next;
	next->prev = pos->prev;
	free(pos);
	pos = NULL;
}

void DLTPopBack(DLTNode* phead)
{
	assert(phead);
	DLTErase(phead->prev);
}

void DLTPopFront(DLTNode* phead)
{
	assert(phead);
	DLTErase(phead->next);
}